<template>
    <div class="app-container">
        <el-row :gutter="20">
            <el-col :xs="24" :sm="24" :lg="12" class="col-item">
                <el-card shadow="always">
                    <div slot="header">
                        <span><strong>CPU</strong></span>
                    </div>
                    <ul>
                        <li class="item">
                            <el-row>
                                <el-col :span="12"><strong>属性</strong></el-col>
                                <el-col :span="12"><strong>值</strong></el-col>
                            </el-row>
                        </li>
                        <li class="item">
                            <el-row>
                                <el-col :span="12">核心数</el-col>
                                <el-col :span="12" v-text="cpu.cpuNum"/>
                            </el-row>
                        </li>
                        <li class="item">
                            <el-row>
                                <el-col :span="12">用户使用率</el-col>
                                <el-col :span="12" v-text="cpu.used+'%'"/>
                            </el-row>
                        </li>
                        <li class="item">
                            <el-row>
                                <el-col :span="12">系统使用率</el-col>
                                <el-col :span="12" v-text="cpu.sys+'%'"/>
                            </el-row>
                        </li>
                        <li class="item">
                            <el-row>
                                <el-col :span="12">当前空闲率</el-col>
                                <el-col :span="12" v-text="cpu.free+'%'"/>
                            </el-row>
                        </li>
                    </ul>
                </el-card>
            </el-col>
            <el-col :xs="24" :sm="24" :lg="12" class="col-item">
                <el-card shadow="always">
                    <div slot="header">
                        <span><strong>内存</strong></span>
                    </div>
                    <ul>
                        <li class="item">
                            <el-row>
                                <el-col :span="8"><strong>属性</strong></el-col>
                                <el-col :span="8"><strong>内存</strong></el-col>
                                <el-col :span="8"><strong>JVM</strong></el-col>
                            </el-row>
                        </li>
                        <li class="item">
                            <el-row>
                                <el-col :span="8">总内存</el-col>
                                <el-col :span="8" v-text="mem.total+'G'"/>
                                <el-col :span="8" v-text="jvm.total+'M'"/>
                            </el-row>
                        </li>
                        <li class="item">
                            <el-row>
                                <el-col :span="8">已用内存</el-col>
                                <el-col :span="8" v-text="mem.used+'G'"/>
                                <el-col :span="8" v-text="jvm.used+'M'"/>
                            </el-row>
                        </li>
                        <li class="item">
                            <el-row>
                                <el-col :span="8">剩余内存</el-col>
                                <el-col :span="8" v-text="mem.free+'G'"/>
                                <el-col :span="8" v-text="jvm.free+'M'"/>
                            </el-row>
                        </li>
                        <li class="item">
                            <el-row>
                                <el-col :span="8">使用率</el-col>
                                <el-col :span="8" v-text="mem.usage+'%'"/>
                                <el-col :span="8" v-text="jvm.usage+'%'"/>
                            </el-row>
                        </li>
                    </ul>
                </el-card>
            </el-col>
        </el-row>
        <el-row class="row">
            <el-card shadow="always">
                <div slot="header">
                    <span><strong>服务器信息</strong></span>
                </div>
                <ul>
                    <li class="item">
                        <el-row>
                            <el-col :xs="12" :sm="12" :lg="6">服务器名称</el-col>
                            <el-col :xs="12" :sm="12" :lg="6" v-text="sys.computerName"/>
                            <el-col :xs="12" :sm="12" :lg="6">操作系统</el-col>
                            <el-col :xs="12" :sm="12" :lg="6" v-text="sys.osName"/>
                        </el-row>
                    </li>
                    <li class="item">
                        <el-row>
                            <el-col :xs="12" :sm="12" :lg="6">服务器IP</el-col>
                            <el-col :xs="12" :sm="12" :lg="6" v-text="sys.computerIp"/>
                            <el-col :xs="12" :sm="12" :lg="6">系统架构</el-col>
                            <el-col :xs="12" :sm="12" :lg="6" v-text="sys.osArch"/>
                        </el-row>
                    </li>
                </ul>
            </el-card>
        </el-row>
        <el-row class="row">
            <el-card shadow="always">
                <div slot="header">
                    <span><strong>缓存信息</strong></span>
                </div>
                <ul>
                    <li class="item">
                        <el-row>
                            <el-col :xs="12" :sm="12" :lg="6">缓存类型</el-col>
                            <el-col :xs="12" :sm="12" :lg="6" v-text="cache.type"/>
                            <el-col :xs="12" :sm="12" :lg="6">缓存IP地址</el-col>
                            <el-col :xs="12" :sm="12" :lg="6" v-text="cache.ip"/>
                        </el-row>
                    </li>
                    <li class="item">
                        <el-row>
                            <el-col :xs="12" :sm="12" :lg="6">缓存端口</el-col>
                            <el-col :xs="12" :sm="12" :lg="6" v-text="cache.port"/>
                            <el-col :xs="12" :sm="12" :lg="6">缓存系统OS</el-col>
                            <el-col :xs="12" :sm="12" :lg="6" v-text="cache.os"/>
                        </el-row>
                    </li>
                    <li class="item">
                        <el-row>
                            <el-col :xs="12" :sm="12" :lg="6">版本</el-col>
                            <el-col :xs="12" :sm="12" :lg="6" v-text="cache.version"/>
                            <el-col :xs="12" :sm="12" :lg="6">已经运行时间</el-col>
                            <el-col :xs="12" :sm="12" :lg="6"  v-text="formatTime(cache.uptimeInSeconds)"/>
                        </el-row>
                    </li>
                    <li class="item">
                        <el-row>
                            <el-col :xs="12" :sm="12" :lg="6">连接数</el-col>
                            <el-col :xs="12" :sm="12" :lg="6" v-text="cache.connectedClients"/>
                            <el-col :xs="12" :sm="12" :lg="6">运行模式</el-col>
                            <el-col :xs="12" :sm="12" :lg="6" v-text="cache.mode==='standalone'?'单机':'集群'"/>
                        </el-row>
                    </li>
                    <li class="item">
                        <el-row>
                            <el-col :xs="12" :sm="12" :lg="6">内存总数</el-col>
                            <el-col :xs="12" :sm="12" :lg="6"  v-text="formatMem(cache.total)"/>
                            <el-col :xs="12" :sm="12" :lg="6">已用内存</el-col>
                            <el-col :xs="12" :sm="12" :lg="6"  v-text="formatMem(cache.used)"/>
                        </el-row>
                    </li>
                    <li class="item">
                        <el-row>
                            <el-col :xs="12" :sm="12" :lg="6">cpu使用率</el-col>
                            <el-col :xs="12" :sm="12" :lg="6" v-text="parseFloat(cache.usedCpu).toFixed(2)+'%'"/>
                            <el-col :xs="12" :sm="12" :lg="6">Key数量</el-col>
                            <el-col :xs="12" :sm="12" :lg="6" v-text="cache.dbSize"/>
                        </el-row>
                    </li>
                </ul>
            </el-card>
        </el-row>
        <el-row class="row">
            <el-card shadow="always">
                <div slot="header">
                    <span><strong>Java虚拟机信息</strong></span>
                </div>
                <ul>
                    <li class="item">
                        <el-row>
                            <el-col :xs="12" :sm="12" :lg="6">Java名称</el-col>
                            <el-col :xs="12" :sm="12" :lg="6" v-text="jvm.name"/>
                            <el-col :xs="12" :sm="12" :lg="6">Java版本</el-col>
                            <el-col :xs="12" :sm="12" :lg="6" v-text="jvm.version"/>
                        </el-row>
                    </li>
                    <li class="item">
                        <el-row>
                            <el-col :xs="12" :sm="12" :lg="6">启动时间</el-col>
                            <el-col :xs="12" :sm="12" :lg="6" v-text="jvm.startTime"/>
                            <el-col :xs="12" :sm="12" :lg="6">运行时长</el-col>
                            <el-col :xs="12" :sm="12" :lg="6" v-text="jvm.runTime"/>
                        </el-row>
                    </li>
                    <li class="item">
                        <el-row>
                            <el-col :xs="12" :sm="12" :lg="6">安装路径</el-col>
                            <el-col :xs="12" :sm="12" :lg="6" v-text="jvm.home"/>
                            <el-col :xs="12" :sm="12" :lg="6">项目路径</el-col>
                            <el-col :xs="12" :sm="12" :lg="6" v-text="sys.userDir"/>
                        </el-row>
                    </li>
                </ul>
            </el-card>
        </el-row>

        <el-row class="row">
            <el-card shadow="always">
                <div slot="header">
                    <span><strong>磁盘状态</strong></span>
                </div>
                <ul>
                    <li class="item">
                        <el-row>
                            <el-col :xs="12" :sm="12" :lg="5"><strong>盘符路径</strong></el-col>
                            <el-col :xs="12" :sm="12" :lg="3"><strong>文件系统</strong></el-col>
                            <el-col :xs="12" :sm="12" :lg="4"><strong>盘符类型</strong></el-col>
                            <el-col :xs="12" :sm="12" :lg="3"><strong>总大小</strong></el-col>
                            <el-col :xs="12" :sm="12" :lg="3"><strong>可用大小</strong></el-col>
                            <el-col :xs="12" :sm="12" :lg="3"><strong>已用大小</strong></el-col>
                            <el-col :xs="12" :sm="12" :lg="3"><strong>使用百分比</strong></el-col>
                        </el-row>
                    </li>

                    <li class="item" v-for="item in sysFiles" :key="item.dirName">
                        <el-row>
                            <el-col :xs="12" :sm="12" :lg="5" v-text="item.dirName"/>
                            <el-col :xs="12" :sm="12" :lg="3" v-text="item.sysTypeName?item.sysTypeName:'未知'"/>
                            <el-col :xs="12" :sm="12" :lg="4" v-text="item.typeName"/>
                            <el-col :xs="12" :sm="12" :lg="3" v-text="item.total"/>
                            <el-col :xs="12" :sm="12" :lg="3" v-text="item.free"/>
                            <el-col :xs="12" :sm="12" :lg="3" v-text="item.used"/>
                            <el-col :xs="12" :sm="12" :lg="3" v-text="item.usage+'%'"/>
                        </el-row>
                    </li>
                </ul>
            </el-card>
        </el-row>
    </div>
</template>

<script>
    import {getSystem} from '@/api/system.js';
    export default {
        name: "ServiceMonitoring",
        data(){
            return{
                cpu: {
                    cpuNum: 0,
                    total: 0,
                    sys: 0,
                    used: 0,
                    wait: 0,
                    free: 0
                },
                mem: {
                    total: 0,
                    used: 0,
                    free: 0,
                    usage: 0
                },
                jvm: {
                    total: 0,
                    max: 0,
                    free: 0,
                    version: '',
                    home: '',
                    name: '',
                    used: 0,
                    startTime: '',
                    usage: 0,
                    runTime: ''
                },
                sys: {
                    computerName: '',
                    computerIp: '',
                    userDir: '',
                    osName: '',
                    osArch: ''
                },
                cache: {
                    total: 0,
                    used: 0,
                    mode: '',
                    type: '',
                    version: '',
                    os: '',
                    ip: '',
                    port: 0,
                    uptimeInSeconds: 0,
                    connectedClients: 0,
                    usedCpu: 0.0,
                    dbSize: 0,
                },
                sysFiles: []
            }
        },
        created(){
            this.systemDetail();
        },
        methods:{
            formatMem(limit) {
                return this.changeMem(limit);
            },
            formatTime(seconds) {
                return this.parseTime(seconds)
            },
            systemDetail(){
                getSystem().then(response=>{
                    //cpu信息
                    this.cpu=response.data.cpu;
                    //內存相关信息
                    this.mem=response.data.mem;
                    //jvm相关信息
                    this.jvm=response.data.jvm;
                    //服务器相关信息
                    this.sys=response.data.sys;
                    //磁盘相关信息
                    this.sysFiles=response.data.sysFiles;
                    //redis缓存的情况
                    this.cache=response.data.cache;
                });
            }
        }
    }
</script>

<style scoped>

    .col-item {
        text-align:left;
        margin-bottom: 20px;
    }

    .row {
        margin-bottom: 20px;
    }


    ul {
        margin: 0;
        padding: 0;
        list-style-type: none;
    }

    .item {
        color: #606266;
        font-size: 14px;
        padding: 7px 10px;
        border-bottom: 1px solid rgb(231, 234, 236);
    }

</style>